package com.huahang.fashionpress.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huahang.fashionpress.enums.ExceptionEnum;
import com.huahang.fashionpress.exception.AddressException;
import com.huahang.fashionpress.form.FormAddress;
import com.huahang.fashionpress.mapper.AddressMapper;
import com.huahang.fashionpress.model.Address;
import com.huahang.fashionpress.service.IAddressService;
import com.huahang.fashionpress.vo.AddressVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 地址服务层
 * @author : 王翰聪
 * @since : 2019/08/25
 * @version : 1.0
 */
@Service
public class AddressServiceImpl implements IAddressService {
    @Autowired
    private AddressMapper addressMapper;

    /**
     * 添加一个地址
     * @param formAddress 用户从前端传回的表单地址对象
     * @return
     */
    @Override
    public boolean addNewAddress(FormAddress formAddress) throws AddressException {
        //如果用户地址超过5个 抛出异常
        if(addressMapper.selectAllAdress(formAddress.getUserID()).size() >= 5){
            throw new AddressException(ExceptionEnum.TOO_MANY_ADDRESS);
        }
        //验证收货地址是否重复
        if(!addressMapper.selectAllAdressByAddressName(formAddress.getUserID(), formAddress.getAddressPlace(),
                formAddress.getAddressName()).isEmpty()){
            throw new AddressException(ExceptionEnum.ADDRESS_ALREADY_EXSIST);
        }
        //查看该地址是否为默认地址
        if(formAddress.getAddressStatus() == 0){
            //查找到之前的默认地址并修改
//            QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("user_id",formAddress.getUserID());
//            queryWrapper.eq("address_status", formAddress.getAddressStatus());
//            List<Address> addressList  = addressMapper.selectList(queryWrapper);
//            Address address = addressList.get(0);
            List<Address> addressList = addressMapper.selectAllAdressByAddressStatus(formAddress.getUserID(), formAddress.getAddressStatus());
            if(!addressList.isEmpty()){
                Address address = addressMapper.selectAllAdressByAddressStatus(formAddress.getUserID(), formAddress.getAddressStatus()).get(0);
                addressMapper.updateAdressStatus(1, address.getAddressId());
            }
        }

        //添加新地址
        int result = addressMapper.insertNewAddress(formAddress.getAddressBase(), formAddress.getAddressPlace(),
                formAddress.getAddressName(), formAddress.getAddressPhone(),
                formAddress.getAddressStatus(), formAddress.getUserID());
        if(result != 1)
            throw new AddressException(ExceptionEnum.ADDRESS_ERROR);
        return true;
    }

    /**
     * 删除地址
     * @param addressID 地址id
     * @return
     */
    @Override
    public boolean deleteAddress(Integer addressID) {
        int result = addressMapper.deleteAddressByAddressID(addressID);
        if(result != 1)
            throw new AddressException(ExceptionEnum.ADDRESS_ERROR);
        return true;
    }

    /**
     * 显示用户所有地址
     * @param userID
     * @return
     */
    @Override
    public List<AddressVO> getAllAddress(Integer userID) {
        List<Address> addressList = addressMapper.selectAllAdress(userID);
        if(addressList.isEmpty())
            return null;
        List<AddressVO> addressVOList = new ArrayList<AddressVO>();
        for(Address address : addressList){
            AddressVO addressVO = new AddressVO();
            addressVO.setAddressBase(address.getAddressBase());
            addressVO.setAddressId(address.getAddressId());
            addressVO.setAddressName(address.getAddressName());
            addressVO.setAddressPlace(address.getAddressPlace());
            addressVO.setAddressPhone(address.getAddressPhone());
            addressVO.setAddressStatus(address.getAddressStatus());

            addressVOList.add(addressVO);
        }
        return addressVOList;
    }

    /**
     * 修改用户地址信息
     * @param formAddress 要修改的地址信息内容
     * @param addressId
     * @return
     */
    @Override
    public boolean changeAddress(FormAddress formAddress, Integer addressId) {
        //根据用户地址id查找要修改的用户地址
        Address address = addressMapper.selectById(formAddress.getAddressID());
        //如果要将该地址修改为默认地址
        if(formAddress.getAddressStatus() != null && formAddress.getAddressStatus() == 0 && address.getAddressStatus() != 0){
            //先将该用户的其他默认地址修改为非默认地址
            Address target = addressMapper.selectAllAdressByAddressStatus(formAddress.getUserID(), formAddress.getAddressStatus()).get(0);
            addressMapper.updateAdressStatus(1, target.getAddressId());
        }
        //修改地址字段
        if(formAddress.getAddressBase() == null || !formAddress.getAddressBase().isEmpty()){
            address.setAddressBase(formAddress.getAddressBase());
            address.setAddressPlace(formAddress.getAddressPlace());
        }
        if(formAddress.getAddressName() == null || !formAddress.getAddressName().isEmpty()){
            address.setAddressName(formAddress.getAddressName());
        }
        if(formAddress.getAddressPhone() == null || !formAddress.getAddressPhone().isEmpty()){
            address.setAddressPhone(formAddress.getAddressPhone());
        }

        //修改地址
        addressMapper.updateById(address);

        return true;
    }
}
